﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;

namespace FindMyChamp
{
    public static class GeoData
    {
        public static event EventHandler PositionChanged;
        public static event EventHandler AfterPositionChanged;
        private static GeoCoordinateWatcher GeoWatcher = new GeoCoordinateWatcher { MovementThreshold = 100.0f };

        public static GeoCoordinate Location
        {
            get { return GeoWatcher.Position.Location; }
        }

        public static GeoPositionStatus Status
        {
            get { return GeoWatcher.Status; }
        }

        static GeoData()
        {
            // if the user has opted out

            if (System.IO.IsolatedStorage.IsolatedStorageSettings.ApplicationSettings.Contains("EnableLocation")
                && ((bool?)System.IO.IsolatedStorage.IsolatedStorageSettings
                    .ApplicationSettings["EnableLocation"] ?? true) == false)
                return;

            StartOrStop();
            GeoWatcher.PositionChanged += (s, e) => { RaisePositionChanged(); };

            // fire anyway @ start
            RaisePositionChanged();
        }

        public static void RaisePositionChanged()
        {
            if (PositionChanged != null)
                PositionChanged(GeoWatcher, EventArgs.Empty);
            if (AfterPositionChanged != null)
                AfterPositionChanged(GeoWatcher, EventArgs.Empty);
        }

        public static void StartOrStop()
        {
            try
            {
                if (Settings.EnableLocation)
                    GeoWatcher.TryStart(false, TimeSpan.FromSeconds(5));
                else
                    GeoWatcher.Stop();
            }
            catch { }
        }

        #region Calcs

        // Gets the distance (in km) between two coordinate (lat-long) points
        // Uses the Haversine formula (http://www.movable-type.co.uk/scripts/latlong.html)
        public static double GetDistance(double fromLatitude, double fromLongitude, double toLatitude, double toLongitude)
        {
            int earthRadius = 6371;         // earth's radius (mean) in km
            double chgLat = DegreeToRadians(toLatitude - fromLatitude);
            double chgLong = DegreeToRadians(toLongitude - fromLongitude);
            double a = Math.Sin(chgLat / 2) * Math.Sin(chgLat / 2) + Math.Cos(DegreeToRadians(fromLatitude)) * Math.Cos(DegreeToRadians(toLatitude)) * Math.Sin(chgLong / 2) * Math.Sin(chgLong / 2);
            double c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a));
            double d = earthRadius * c;
            return d;
        }

        private static double DegreeToRadians(double value)
        {
            return Math.PI * value / 180.0;
        }

        #endregion
    }
}
